﻿using System;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using TpAgentCore.Base;
using TpBaseObjects.Base;
using TpLogger;
using TpTools;
using TpIntern.GUI;

namespace TpIntern {
    class Core {

        #region Members

        public const string tpContextMenuName = "Ply";

        private enum faceIDs {
            NewSheet = 343,
            Export = 352,
            Import = 342,
            Dokument = 341,
            Bearbeitung = 345
        }

        #endregion

        #region Kontextmenü

        #region Export + Funktion

        public static void ChangeView(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, Logger log) {
            Agent agent = new Agent();
            wb.Save();
            //Auswahl awAuswahl = new Auswahl();
            SelectForm awAuswahl = new SelectForm();
            awAuswahl.ShowDialog();

            if(awAuswahl.Cancel) {
                return;
            }

            agent.ChangeView(sh, awAuswahl.DateFrom, awAuswahl.DateTo, awAuswahl.Content, awAuswahl.Sorting, awAuswahl.SortOrder, awAuswahl.ShowType);
            wb.Activate();
        }

        public static void ExportJobevidenzIntern(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, Logger log) {
            try {
                Agent agent = new Agent();
                agent.UpdateJobEvidenzIntern(new string[] { wb.FullName }, false);
            } catch(Exception ex) {
                log.Add("ExportJobevidenzIntern(TpIntern)", ex);
            }
        }

        public static void ExportLeistungenIntern(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, Logger log) {
            try {
                Agent agent = new Agent();
                agent.UpdateLeistungenIntern(wb, sh);
            } catch(Exception ex) {
                log.Add("ExportLeistungenIntern(TpIntern)", ex);
            }
        }

        #endregion

        #endregion

        #region Workbook Events

        public static void WBActivate(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, Logger log) {
            SetKontextMenu(exApp, wb, sh, false, log);
        }

        public static void WBDeactivate(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            DeleteMenuEntries(exApp);
        }

        #endregion

        #region Methods

        public static void AddMenuEntries(Logger log) {
            try {
                if(GlobalVar.MenuEntriesIntern.Count == 0) {

                    //View
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 1, GroupHeader = true, MenuName = "ChangeViewGroup", Caption = "Ansicht", FaceID = (int)faceIDs.NewSheet, Enabled = true, NewGroup = true, Visibly = true, EverySheet = true }, true);
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 1, MenuName = "ChangeView", Caption = "Ansicht ändern", OnAction = "VBSub_1", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true, }, true);

                    //Export (int)faceIDs.Export
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 2, GroupHeader = true, MenuName = "GruppeExportIntern", Caption = "Export + Funktion Intern", FaceID = (int)faceIDs.Export, Enabled = true, NewGroup = true, Visibly = true, EverySheet = true }, true);
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 2, MenuName = "ExportLeistungenIntern", Caption = "Leistungen Intern", OnAction = "VBSub_18", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true, }, true);
                    //GlobalVar.AddMenu(new MenuEintrag { GroupID = 2, MenuName = "EinfügenLeistungenIntern", Caption = "Leistungen Intern", OnAction = "VBSub_18", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true }, true);
                }
            } catch(Exception ex) {
                log.Add("AddMenuEntriesIntern", ex);
            }
        }

        private static void DeleteMenuEntries(Microsoft.Office.Interop.Excel.Application exApp) {
            DeleteMenuEntries(exApp, null);
        }

        private static void DeleteMenuEntries(Microsoft.Office.Interop.Excel.Application exApp, string menuName) {
            if(menuName == null) {
                foreach(MenuEintrag me in GlobalVar.MenuEntriesIntern) {
                    if(KontextMenuControlExists(exApp, me.Caption)) {
                        exApp.CommandBars[tpContextMenuName].Controls[me.Caption].Delete();
                    }
                }
            } else {
                foreach(MenuEintrag me in GlobalVar.MenuEntriesIntern) {
                    if(me.Caption == menuName) {
                        if(KontextMenuControlExists(exApp, me.Caption)) {
                            exApp.CommandBars[tpContextMenuName].Controls[me.Caption].Delete();
                        }
                    }
                }
            }
        }

        public static void KontextMenuAddControl(Microsoft.Office.Interop.Excel.Application exApp, Logger log) {

            foreach(MenuEintrag me in GlobalVar.MenuEntriesIntern) {
                try {
                    if(!KontextMenuControlExists(exApp, me.Caption)) {
                        CommandBarButton cbb = (CommandBarButton)exApp.CommandBars[tpContextMenuName].Controls.Add();
                        cbb.Caption = me.Caption;
                        cbb.OnAction = me.OnAction;
                        cbb.FaceId = me.FaceID;
                        cbb.BeginGroup = me.NewGroup;
                        cbb.Enabled = me.Enabled;
                        cbb.Height = 0;
                    }
                } catch(Exception ex) {
                    log.Add("KontextMenuAddControl(" + me.Caption + ")", ex);
                }
            }
        }

        public static void SetKontextMenu(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, bool delete, Logger log) {
            try {
                if(!wb.FullName.Contains("je.xlsm")) {
                    DeleteMenuEntries(exApp);
                    return;
                }
                
                AddMenuEntries(log);

                KontextMenuAddControl(exApp, log);


            } catch(Exception ex) {
                log.Add("SetKontextMenu", ex);
            }
        }

        #endregion

        #region Functions

        private static bool KontextMenuControlExists(Microsoft.Office.Interop.Excel.Application exApp, string caption) {
            foreach(CommandBarButton cbb in exApp.CommandBars[tpContextMenuName].Controls) {
                if(cbb.Caption == caption) {
                    return true;
                }
            }
            return false;
        }

        #endregion

    }
}
